using System;
using System.Collections.Generic;
using System.Text;
using GameStateManager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ScreenTest.Constants;

namespace ScreenTest.Screens
{
    class PauseScreen : MenuScreen
    {
        int playerWhoPaused;
        public delegate void UserResponse(int result);
        public event UserResponse userResponse;

        float scaleOfChoice = 1f;
        bool scaleUp = true;
        const float SCALE_FACTOR = 0.03f;

        public PauseScreen(int playerWhoPaused)
            : base()
        {
            menuItems.Add("Continue");
            menuItems.Add("Quit");

            this.playerWhoPaused = playerWhoPaused;
        }

        public override void HandleInput(InputState inputState, GameTime gameTime)
        {

            if (inputState.MenuItemSelect)
            {
                MenuItemSelected();
            }

            if (inputState.MenuItemDown)
            {
                currentChoice++;

                if (currentChoice > 1)
                    currentChoice = 0;
            }

            if (inputState.MenuItemUp)
            {
                currentChoice--;

                if (currentChoice < 0)
                    currentChoice = 1;
            }
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus)
        {
            if (scaleUp)
            {
                scaleOfChoice += SCALE_FACTOR;
                if (scaleOfChoice > 2f)
                {
                    scaleUp = false;
                }
            }
            else
            {
                scaleOfChoice -= SCALE_FACTOR;

                if (scaleOfChoice < 1.0f)
                {
                    scaleUp = true;
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.FadeBackBufferToBlack(50);

            int spacing = menuFont.LineSpacing;
            Vector2 position = new Vector2(100, 100);

            ScreenManager.SpriteBatch.Begin();
            for (int i = 0; i < menuItems.Count; i++)
            {
                String item = menuItems[i];

                if (i == currentChoice)
                {
                    ScreenManager.SpriteBatch.DrawString(menuFont, item, position, Color.White, 0f, Vector2.Zero, scaleOfChoice, SpriteEffects.None, 0f);
                }
                else
                {
                    ScreenManager.SpriteBatch.DrawString(menuFont, item, position, Color.White);
                }
                position.Y += spacing;
            }
            ScreenManager.SpriteBatch.End();
        }

        void MenuItemSelected()
        {
            switch (currentChoice)
            {
                case 0:
                    userResponse(PauseConstants.PAUSE_CONTINUE);
                    break;

                case 1:
                    userResponse(PauseConstants.PAUSE_QUIT);
                    break;
            }

            ScreenManager.RemoveScreen(this);
        }

    }
}
